package session

import (
	"OnlineRetailers/models/Admin"
	"encoding/json"
	"fmt"
)

func LinkSession(user *Admin.User, session string) (err error) {

	sessionUser := &Admin.Session{SessionId: session, Admin: user}
	exit, err := ExistSession(session)
	fmt.Println(exit, err)
	if err != nil {
		return
	}
	if !exit {
		err = fmt.Errorf("session过期")
		return
	}

	err = DestroySession(session)
	if err != nil {
		return
	}

	sessionJson, err := json.Marshal(sessionUser)
	if err != nil {
		return
	}
	err = NewSession(sessionUser, sessionJson)

	return
}
